<template>
    <el-tree-select node-key="id" v-model="parentId" :data="options2" :props="normalizer" :render-after-expand="false" clearable/>
</template>

<script>
import { getAll } from "@/api/zhongyi/yaocaitag";

export default {
  props: {
    modelValue: {
      required: true,
      default: null,
    },
    type: {
      required: true,
      default: '',
    }
  },
  emits: ['update:modelValue'],
  data() {
    return {
      options: [],
     // yaocaiOptions: [],
     // fangjiOptions: [],
      normalizer: {
          label: 'name',
          children: 'child',
      },
      parentId: null
    };
  },
  async mounted() {
     getAll().then((res) => {
        this.options = res.result
        //this.buildOption(this.options )
        console.log(this.options)
      });

      //this.yaocaiOptions = this.yaocaitags
      //this.fangjiOptions = this.fangjitags
     // console.log(this.$store.state, this.options)
  },
  methods: {
    oninput(val) {
      this.$emit('update:modelValue', val)
    },
    onSelect(node) {
      //debugger
      //this.$emit('input', node.id)
      // this.form.level = node.level + 1;
    },
    buildOption(options) {
      for(let option of options) {
        if(option.child.length == 0) {
          // option.child = null
          delete option.child
        }
        else {
          this.buildOption(option.child )
        }
      }
    },
  },
  watch: {
    modelValue: function (val) {
       // console.log('val',val)
        this.parentId = val
    },
    parentId: function(val) {
      this.$emit('update:modelValue', val)
    }
  },
  computed: {
    options2() {
      if(this.type){
        return this.options.filter(t=>t.type == this.type)
      }
      else {
        //return this.options
        return this.options
      }
    }
  }
};
</script>


